<?php

require_once(WSB_MODULES_PATH . 'cms/models/Containers.php');
require_once(WSB_MODULES_PATH . 'cms/models/Container.php');

class Cms_Model_DbTable_Container extends WSB_Db_Table
{

    protected $_name = 'container';
    protected $_primary = 'id';
    protected $_rowsetClass = 'Cms_Model_Containers';
    protected $_rowClass = 'Cms_Model_Container';
    protected $_dependentTables = array();

	/**
	 * @param integer $page_id
	 * @param integer $placeholder_id
	 * @param integer $language_id
	 */
	public function reorderContainers($page_id, $placeholder_id, $language_id)
	{
		$adapter = $this->getAdapter();

		$data = array('sort' => WSB_Db_Table::SORT_DELETED);
		$where = array(
			$adapter->quoteInto('deleted=?', 1),
			$adapter->quoteInto('sort<>?', WSB_Db_Table::SORT_DELETED)
		);
		$this->update($data, $where);

		$where = array(
			$adapter->quoteInto('deleted=?', 0),
			$adapter->quoteInto('page_id=?', $page_id),
			$adapter->quoteInto('language_id=?', $language_id),
			$adapter->quoteInto('placeholder_id=?', $placeholder_id)
		);
		$i = 0;
		foreach($this->fetchAll($where, 'sort ASC') AS $container) {
			if($i != $container->sort) {
				$container->sort = $i;
				$container->save();
			}
			$i++;
		}
	}

	/**
	 * @param array $container
	 * @return integer
	 */
	public function addContainer(array $container)
	{

		$sql  = 'UPDATE container';
		$sql .= ' SET sort=sort+1';
		$sql .= ' WHERE page_id=' . $container['page_id'];
		$sql .= ' AND language_id=' . $container['language_id'];
		$sql .= ' AND placeholder_id=' . $container['placeholder_id'];
		$sql .= ' AND sort>=' . $container['sort'];
    	$this->getAdapter()->query($sql);

    	$container['recursive'] = 0;
    	$item = $this->createRow($container);
    	$id = $item->save();

    	$this->reorderContainers($container['page_id'], $container['placeholder_id'], $container['language_id']);
    	return $id;
	}

	/**
	 * @param integer $idcontainer
	 * @param string $direction
	 */
	public function moveContainer($idcontainer, $direction='up')
	{
		if(empty($idcontainer) || !is_numeric($idcontainer)) {
			throw new Exception('Argument $idcontainer muss vom Typ Integer sein.');
		}
		$where = $this->getAdapter()->quoteInto('id=?', $idcontainer);
		$srcContainer = $this->fetchRow($where);

		if($srcContainer instanceof Cms_Model_Container) {
			$where = sprintf(
				'page_id=%d AND placeholder_id=%d AND language_id=%d AND deleted=0',
				$srcContainer->page_id,
				$srcContainer->placeholder_id,
				$srcContainer->language_id
			);

			$i = 0;
			foreach($this->fetchAll($where, 'sort ASC') AS $container) {
				// Updaten fals Sortierreihenfolge nicht stimmt
				if($container->sort != $i) {
					$container->sort = $i;
					$container->save();
				}
				if($direction == 'up') {
					if($i == ($srcContainer->sort-1)) {
						$dstContainer = $container;
					}
				} else {
					if($i == ($srcContainer->sort+1)) {
						$dstContainer = $container;
					}
				}
				$i++;
			}

			$dstSort = $dstContainer->sort;
			$srcSort = $srcContainer->sort;

			$dstContainer->sort = $srcSort;
			$dstContainer->save();

			$srcContainer->sort = $dstSort;
			$srcContainer->save();

			$this->reorderContainers($srcContainer->page_id, $srcContainer->placeholder_id, $srcContainer->language_id);
		}

	}

	/**
	 * @param integer $idcontainer
	 */
	public function deleteContainer($idcontainer)
	{
		if(empty($idcontainer) || !is_numeric($idcontainer)) {
			throw new Exception('Argument $idcontainer muss vom Typ Integer sein.');
		}
		$where = $this->getAdapter()->quoteInto('id=?', $idcontainer);
		$container = $this->fetchRow($where);
		$container->deleted = 1;
		$container->save();
		$this->reorderContainers($container->page_id, $container->placeholder_id, $container->language_id);
	}

}